<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars
define("PRIMARY_KEY","kurssi_id");
$FileName        = "ais_transaction_currsirate.php";
$FileTemplate    = "ais_transaction_currsirate_form.html";
$HTMLFormName    = "CurrencyForm";
$TableName       = "ais_kurssi";
$SubHeaderTitle  = "Transaction Currency Rate - Rate of days";
$FormAction      = $FileName . "?" . AddParam(GetQueryString("QueryString", ""),"form","$HTMLFormName");
$FormEncType     = "application/x-www-form-urlencoded";
//$PMode         = "";

//Initiate Database Table For Insert & Update;
$FormFields     = array("tglbkt"=>"date",
                        "curr"=>"string",
                        "rate"=>"number"
                        );
buildsqlfield ($TableName, $FormFields);

//Leave Here Untouch
$FormCName     = BuildHTMLField($FormFields,"name",1);
$FormCValue    = BuildHTMLField($FormFields,"value",1);

//************ FORM ACTION **************
if (GetParam("form","")==$HTMLFormName) {
    if ((GetParam("Action","")=="Add") || (GetParam("Action","")=="Add & Update All Transaction")) {
        $FormMode="Edit";
        FormInsert();
        SetLogActivity(GetParam("curr",""),"Create",GetParam("rate",""));
        if ((GetParam("Action","")=="Add & Update All Transaction")) {
            UpdateTrans();
            SetLogActivity(GetParam("curr",""),"Create",GetParam("rate","")."  (".GetParam("Action","").")");
        }
    }

    if ((GetParam("Action","")=="Update") || (GetParam("Action","")=="Update All Transaction")) {
        $FormMode="Edit";
        FormUpdate();
        if ((GetParam("Action","")=="Update All Transaction")) {
            UpdateTrans();
            SetLogActivity(GetParam("curr",""),"Update",GetParam("rate","")."  (".GetParam("Action","").")");
        }
    }

    if (GetParam("Action","")=="Delete") {
        $FormMode="Edit";
        FormDelete();
        SetLogActivity(GetParam("curr",""),"Delete",GetParam("rate","")."");
    }

    if (GetParam("Action","")=="Cancel") {
        $FormMode="Edit";
        FormCancel();
    }
}

//Field Verification
$Verification  = "Enable";
$SessionVar    = md5(microtime());
//$SessionVar    = "Stuck";
$VerifyFields  = array("tglbkt"=>"Date","curr"=>"Currency","rate"=>"Rate");
VerifyHTML ($HTMLFormName,$VerifyFields,$SessionVar);

$InitFunction = "Masking()";
Init($SessionVar,BuildMasking ($HTMLFormName,$FormFields));
//************ SET FORM MODE ***************

if (GetParam(PRIMARY_KEY,"")) $FormMode = "Edit";
else $FormMode = "Add";

//******* DATABASE FORM QUERY ***********
//Rama, (01 September 2009), Start
//modified SQL, add one condition after "WHERE" clause
$SQLCurrency = "SELECT * FROM tblcurrency WHERE tusymbol NOT LIKE 'IDR'";
//Rama, (01 September 2009), End

$SQL = "SELECT * FROM `$TableName` ";
if (GetParam(PRIMARY_KEY,"")) {
    $SQLForm = $SQL."WHERE ".PRIMARY_KEY."='".GetParam(PRIMARY_KEY,"")."'";
}
//******* SHOW FORM CONTENT *********
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);

$TBS->MergeBlock("currblk",$DBMysql,$SQLCurrency);

if (GetParam(PRIMARY_KEY,"")) $TBS->MergeBlock("blv","mysql",$SQLForm);
else $TBS->MergeBlock("blv",$FormCValue);
//HTML Name
$TBS->MergeBlock("blc",$DBMysql,$SQL);
$TBS->MergeBlock("bln",$FormCName);
//HTMLGrid
$TBS->MergeBlock("blg",$DBMysql,$SQL);
$TBS->Show();


//*************** ACTION FUNCTION *******************
function getNextCurrUpdate(){
        global $TableName,$DBConnection;
        list($day,$month,$year) = explode("/",GetParam("tglbkt",""));
        $df = $year . '-' . $month . '-' . $day;
        $SQL = "SELECT DATE_FORMAT(tglbkt,'%Y-%m-%d') nextDay FROM $TableName WHERE tglbkt > '$df' AND curr='".GetParam('curr','')."' ORDER BY tglbkt";
        $result = $DBConnection->dbc->get_row($SQL);
        return $result->nextDay;
}

function UpdateTrans() {
    global $DBConnection;
    list($day,$month,$year) = explode("/",GetParam("tglbkt",""));
        $df = $year . '-' . $month . '-' . $day;
        $nextDay = getNextCurrUpdate();

    $SQL = "UPDATE ais_advance SET rate='".StrToNumber(GetParam("rate",""))."',rupiah=jumlah*".StrToNumber(GetParam("rate",""))." WHERE tglbkt >= '$df' AND tglbkt < '$nextDay' AND curr='".GetParam("curr","")."'";
    $DBConnection->dbc->query($SQL);

    $SQL = "UPDATE ais_receipt SET rate='".StrToNumber(GetParam("rate",""))."',rupiah=jumlah*".StrToNumber(GetParam("rate",""))." WHERE tglbkt >= '$df' AND tglbkt < '$nextDay' AND curr='".GetParam("curr","")."'";
    $DBConnection->dbc->query($SQL);

    $SQL = "UPDATE ais_piutang SET rate='".StrToNumber(GetParam("rate",""))."',rupiah=jumlah*".StrToNumber(GetParam("rate",""))." WHERE tglbkt >= '$df' AND tglbkt < '$nextDay' AND curr='".GetParam("curr","")."'";
    $DBConnection->dbc->query($SQL);

    $SQL = "UPDATE ais_piutang_z SET rate='".StrToNumber(GetParam("rate",""))."',rupiah=jumlah*".StrToNumber(GetParam("rate",""))." WHERE tglbkt >= '$df' AND tglbkt < '$nextDay' AND curr='".GetParam("curr","")."'";
    $DBConnection->dbc->query($SQL);

    $SQL = "UPDATE ais_hutang SET rate='".StrToNumber(GetParam("rate",""))."',rupiah=jumlah*".StrToNumber(GetParam("rate",""))." WHERE tglbkt >= '$df' AND tglbkt < '$nextDay' AND curr='".GetParam("curr","")."'";
    $DBConnection->dbc->query($SQL);

    $SQL = "UPDATE ais_hutang_z SET rate='".StrToNumber(GetParam("rate",""))."',rupiah=jumlah*".StrToNumber(GetParam("rate",""))." WHERE tglbkt >= '$df' AND tglbkt < '$nextDay' AND curr='".GetParam("curr","")."'";
    $DBConnection->dbc->query($SQL);

    $SQL = "UPDATE ais_memorial SET rate='".StrToNumber(GetParam("rate",""))."',rupiah=jumlah*".StrToNumber(GetParam("rate",""))." WHERE tglbkt >= '$df' AND tglbkt < '$nextDay' AND curr='".GetParam("curr","")."'";
    $DBConnection->dbc->query($SQL);

    $SQL = "UPDATE ais_memorial_z SET rate='".StrToNumber(GetParam("rate",""))."',rupiah=jumlah*".StrToNumber(GetParam("rate",""))." WHERE tglbkt >= '$df' AND tglbkt < '$nextDay' AND curr='".GetParam("curr","")."'";
    $DBConnection->dbc->query($SQL);

}

function FormInsert() {
    global $DBConnection,$FileName;
    $DBConnection->Insert();
    header("Location: $FileName?PageMode=Form");
    exit;
}

function FormUpdate() {
    global $DBConnection;
    $DBConnection->Update();
    list($day,$month,$year) = explode("/",GetParam("tglbkt",""));
    $df = $year . '-' . $month . '-' . $day;
    $nextDay = getNextCurrUpdate();
//die($nextDay);
    $SQL = "UPDATE ais_memorial SET rate='".StrToNumber(GetParam("rate",""))."', rupiah=".StrToNumber(GetParam("rate",""))."*jumlah WHERE tglbkt >= '$df' AND tglbkt < '$nextDay' AND curr='".GetParam("curr","")."'";
//die($SQL);
    $DBConnection->dbc->query($SQL);
//  $DBConnection->dbc->debug();
    $SQL = "UPDATE ais_piutang SET rate='".StrToNumber(GetParam("rate",""))."', rupiah=".StrToNumber(GetParam("rate",""))."*jumlah WHERE tglbkt >= '$df' AND tglbkt < '$nextDay' AND curr='".GetParam("curr","")."'";

    $DBConnection->dbc->query($SQL);
//  $DBConnection->dbc->debug();
    $SQL = "UPDATE ais_hutang SET rate='".StrToNumber(GetParam("rate",""))."', rupiah=".StrToNumber(GetParam("rate",""))."*jumlah WHERE tglbkt >= '$df' AND tglbkt < '$nextDay' AND curr='".GetParam("curr","")."'";

    $DBConnection->dbc->query($SQL);
//  $DBConnection->dbc->debug();

}

function FormDelete() {
    global $DBConnection,$FileName,$TableName;
    $DBConnection->Delete("$TableName",PRIMARY_KEY);
    header("Location: $FileName");
    exit;

}

function FormSearch () {

}

function FormCancel () {
    global $FileName;
    header("Location: ".$FileName);
    exit;
}
//*************** USER FUNCTION *******************
function buildsqlfield ($table, $arrayfield) {
        global $DBConnection;
        foreach ($arrayfield as $key=>$Formatting) {
            $value = GetParam($key,"");
            if ($Formatting=="date") {
                list($day,$month,$year) = explode("/",$value);
                $value = "$year-$month-$day";
            } elseif ($Formatting=="number") $value = str_replace(",","",$value);
            $DBConnection->add_htmlfield($table,$key,$value);
        }
}

?>
